<template>
  <div>
    <el-main>
      <el-button type="primary" plain icon="el-icon-plus" @click="addAdmin()"
        >添加用户</el-button
      >
      <el-table :data="tableData">
        <el-table-column fixed prop="id" label="ID" width="140">
        </el-table-column>
        <el-table-column prop="nickname" label="姓名" width="120">
        </el-table-column>
        <el-table-column prop="email" label="邮箱" width="300">
        </el-table-column>
        <el-table-column prop="status" label="状态"
          ><template slot-scope="scope">
            <span v-if="scope.row.status === 0" style="color: #909399"
              >未激活</span
            >
            <span v-if="scope.row.status === 1" style="color: #67c23a"
              >正常使用</span
            >
            <span v-if="scope.row.status === 2" style="color: #f56c6c"
              >禁用</span
            >
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <span v-if="scope.row.status === 0"
              ><el-button
                type="success"
                icon="el-icon-check"
                plain
                @click="enableAdmin(scope.row)"
                size="small"
                >启用账号</el-button
              >
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="small"
                plain
                @click="deleteAdmin(scope.row)"
                >删除账号</el-button
              ></span
            >
            <span v-if="scope.row.status === 1"
              ><el-button
                @click="disableAdmin(scope.row)"
                type="warning"
                icon="el-icon-close"
                size="small"
                plain
                >禁用账号</el-button
              >
              <el-button
                type="primary"
                icon="el-icon-refresh"
                size="small"
                plain
                @click="resetPass(scope.row)"
                >重置密码</el-button
              ></span
            >
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <!-- pagination -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="current"
      :page-sizes="[5, 10, 20]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <el-dialog title="新增用户" :visible.sync="addFormVisible">
      <el-form :model="ruleForm">
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-select v-model="ruleForm.disabled" placeholder="请选择状态">
            <el-option label="启用" value="false"></el-option>
            <el-option label="禁用" value="true"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAdd()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- editForm -->
    <el-dialog title="编辑用户" :visible.sync="editFormVisible">
      <el-form :model="ruleForm">
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-select v-model="ruleForm.disabled" placeholder="请选择状态">
            <el-option label="启用" value="false"></el-option>
            <el-option label="禁用" value="true"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleEdit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  getInfo,
  addAdmin,
  editAdmin,
  resetPass,
  deleteAdmin,
  enableAdmin,
  disableAdmin,
} from "@/api/user";
import router from "@/router/routers";

export default {
  name: "User",
  data() {
    return {
      loadding: false,
      addFormVisible: false,
      editFormVisible: false,
      formLabelWidth: "120px",
      currentId: localStorage.getItem("ID"),
      currentUser: localStorage.getItem("NAME"),
      ruleForm: {
        id: "",
        username: "",
        password: "",
        disabled: "",
      },
      tableData: null,
      current: 1,
      size: 5,
      total: null,
    };
  },
  mounted() {
    this.handleCurrentChange(1);
  },
  methods: {
    logOut() {
      console.log("退出");
      localStorage.clear();
      router.push({ path: "/login" });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      getInfo(this.current, val)
        .then((res) => {
          console.log(res);
          this.tableData = res.records;
          this.total = res.total;
          this.current = res.current;
          this.size = res.size;
        })
        .catch((error) => {
          console.log(error);
        });
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      getInfo(val, this.size).then((res) => {
        console.log(res);
        this.tableData = res.records;
        this.total = res.total;
        this.current = res.current;
        this.size = res.size;
      });
    },
    addAdmin() {
      this.addFormVisible = true;
      this.ruleForm.username = null;
      this.ruleForm.disabled = null;
    },
    handleAdd() {
      this.addFormVisible = false;
      console.log("add");
      addAdmin(
        this.ruleForm.username,
        this.ruleForm.password,
        this.ruleForm.disabled
      ).then((res) => {
        console.log(res);
        this.$message("添加成功");
        this.addFormVisible = false;
      });
    },
    editAdmin(val) {
      console.log(val);
      this.ruleForm.id = this.currentId;
      this.ruleForm.username = this.currentUser;
      this.editFormVisible = true;
    },
    handleEdit() {
      this.editFormVisible = false;
      console.log("edit");
      editAdmin(this.ruleForm.id, this.ruleForm.password).then((res) => {
        console.log(res);
        this.$message("修改完成");
        this.editFormVisible = false;
      });
    },
    resetPass(val) {
      console.log("密码重置");
      this.$confirm("确定重置密码吗 ?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          resetPass(val.id).then((res) => {
            console.log(res);
            this.$message("重置成功");
            this.handleCurrentChange(this.current);
          });
        })
        .catch(() => {});
    },
    disableAdmin(val) {
      console.log("禁用");
      this.$confirm("确定禁用吗 ?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          disableAdmin(val.id).then((res) => {
            console.log(res);
            this.$message("禁用成功");
            this.handleCurrentChange(this.current);
          });
        })
        .catch(() => {});
    },
    enableAdmin(val) {
      console.log("启用");
      this.$confirm("确定启用吗 ?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          enableAdmin(val.id).then((res) => {
            console.log(res);
            this.$message("启用成功");
            this.handleCurrentChange(this.current);
          });
        })
        .catch(() => {});
    },
    deleteAdmin(val) {
      console.log("删除");
      this.$confirm("确定删除吗 ?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteAdmin(val.id).then((res) => {
            console.log(res);
            this.$message("删除成功");
            this.handleCurrentChange(this.current);
          });
        })
        .catch(() => {});
    },
  },
};
</script>